//
//  XLLabel.m
//  campus
//
//  Created by xl on 2019/11/11.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLLabel.h"

@interface XLLabel ()

@property (nonatomic, weak) id<XLLabelDelegate> delegate;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;

@end

@implementation XLLabel

- (void)changLineSpacing
{
    [self changLineSpacing:8];
}

- (void)changLineSpacing:(CGFloat)padding
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = padding; // 字体的行间距
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{NSFontAttributeName:self.font,
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    self.attributedText = [[NSAttributedString alloc] initWithString:self.text attributes:attributes];
}

- (void)addUnderlineStyleSignleWithCilcked:(id)delegate
{
    //下划线
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:self.text attributes:attribtDic];
    self.attributedText = attributedText;
    
    if (delegate) {
        self.delegate = delegate;
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:self.tapGesture];
    }
}

#pragma mark - buttonEvent
- (void)labelTextCliked:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(labeTextClicked)]) {
        [self.delegate labeTextClicked];
    }
}

#pragma mark - setter/getter
- (UITapGestureRecognizer *)tapGesture
{
    if (!_tapGesture) {
        _tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelTextCliked:)];
        _tapGesture.cancelsTouchesInView = YES;
        _tapGesture.numberOfTapsRequired = 1;
    }
    return _tapGesture;
}

@end
